<?php

namespace app\erp\model;

use think\Model;

class JCartdb extends Model
{
    protected $connection = 'database.juncai_db';
    protected $pk = 'cart_id';
    protected $name = 'cart';
    public $param = [];
    public $search_count = 0;
    public function goods()
    {
        return $this->hasOne('Goodsdb', 'goods_sku', 'goods_sku');
    }

    // 批量修改，打包，出库等状态
    public function editCartState() {
        switch ($this->param ['field']) {
            case 'out_state' :
                // 出库
                $data ['out_state'] = 1;
                $this->where ( 'cart_id', 'IN', $this->param ['cart_ids'] )->update ( $data );
                $this->param ['out_state'] = 2;
                break;
            case 'entry_state' :
                // 入库
                $data ['entry_state'] = 1;
                $this->where ( 'cart_id', 'IN', $this->param ['cart_ids'] )->update ( $data );
                $this->param ['entry_state'] = 2;
                break;
            case 'purchase_state' :
                // 采购
                $data ['purchase_state'] = 1;
                $data ['depot_id'] = $this->param ['depot_id'];
                $data ['cost_price'] = $this->param ['cost_price'];
                $this->where ( 'cart_id', 'IN', $this->param ['cart_ids'] )->update ( $data );
                $this->param ['purchase_state'] = 2;
                break;
            case 'pack_id' :
                // $data['depot_id'] = $this->param['depot_id'];
                $data ['pack_id'] = $this->param ['value'];
                $data ['pack_state'] = 1;
                $this->where ( 'cart_id', 'IN', $this->param ['cart_ids'] )->update ( $data );
                // 查询是否还有未打包状态
                $this->param ['pack_state'] = 2;
                break;
            case 'receipt_state' :
                // 收货
                $data ['receipt_state'] = 1;
                $data ['receipt_time'] = time ();
                $this->where ( 'cart_id', 'IN', $this->param ['cart_ids'] )->update ( $data );
                break;
            case 'invoice_state' :
                // 开票
                $data ['invoice_state'] = 1;
                $this->where ( 'cart_id', 'IN', $this->param ['cart_ids'] )->update ( $data );
                $this->param ['invoice_state'] = 2;
                $this->count ();
                break;
            case 'pay_state' :
                // 支付状态
                $data ['pay_state'] = 1;
                $data ['pay_time'] = time ();
                $this->where ( 'cart_id', 'IN', $this->param ['cart_ids'] )->update ( $data );
                $this->param ['pay_state'] = 2;
                break;
            case 'judge_state' :
                $data ['judge_state'] = 1;
                $this->where ( 'cart_id', 'IN', $this->param ['cart_ids'] )->update ( $data );
                $this->param ['judge_state'] = 2;
                break;
        }
    }
}